package com.fh.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.pms.entity.ProductAttribute;
import com.fh.pms.service.IProductAttributeService;
import com.fh.result.ResultObject;
import com.fh.search.ProductAttributeBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author F
 * @since 2021-05-21
 */
@RestController
@RequestMapping("/productAttributes")
public class ProductAttributeController {

    @Autowired
    private IProductAttributeService productAttributeService;


    @GetMapping
    public ResultObject queryList(ProductAttributeBo productAttributeBo){
        QueryWrapper<ProductAttribute> productAttributeBoQueryWrapper = new QueryWrapper<>();
        if(productAttributeBo.getPid()!=null){
            productAttributeBoQueryWrapper.eq("product_attribute_category_id",productAttributeBo.getPid());
        }
        if(productAttributeBo.getType()!=null){
            productAttributeBoQueryWrapper.eq("type",productAttributeBo.getType());
        }
        productAttributeService.page(productAttributeBo,productAttributeBoQueryWrapper);
        return ResultObject.success(productAttributeBo);
    }

    @PostMapping
    public ResultObject saveEditProductAttribute(@RequestBody ProductAttribute productAttribute){
        productAttributeService.saveEditProductAttribute(productAttribute);
        return ResultObject.success();
    }
    @GetMapping("/{id}")
    public ResultObject queryAttributeById(@PathVariable("id") Long id){
        return ResultObject.success(productAttributeService.getById(id));
    }
    @DeleteMapping("/{id}")
    public ResultObject deleteAttributeById(@PathVariable("id") Long id){
        productAttributeService.removeById(id);
        return ResultObject.success();
    }
    @GetMapping("/{cateId}/{type}")
    public ResultObject  findAttributeList(@PathVariable("cateId") Long cateId,@PathVariable("type") Integer type){
        QueryWrapper<ProductAttribute> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_attribute_category_id", cateId);
        queryWrapper.eq("type",type);
        List<ProductAttribute>list=productAttributeService.list(queryWrapper);
        return ResultObject.success(list);
    }

}
